package cn.dnui_lxx432.dnuifood_16110100432.controller;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_lxx432.dnuifood_16110100432.Adapter.CommentAdapter;
import cn.dnui_lxx432.dnuifood_16110100432.Adapter.FoodAdapter;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.FoodBean;
import cn.dnui_lxx432.dnuifood_16110100432.JavaBean.comments;
import cn.dnui_lxx432.dnuifood_16110100432.R;
import cn.dnui_lxx432.dnuifood_16110100432.Other.Purchase;
import cn.dnui_lxx432.dnuifood_16110100432.listener.OnRetrofitListener;
import cn.dnui_lxx432.dnuifood_16110100432.model.CollectModel;
import cn.dnui_lxx432.dnuifood_16110100432.model.FoodModel;
import cn.dnui_lxx432.dnuifood_16110100432.model.UserModel;
import cn.dnui_lxx432.dnuifood_16110100432.service.ShopService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class FoodInfoActivity extends BaseActivity implements OnRetrofitListener <FoodBean>{

    private Context mContext;
    private Menu menu;
    private FoodBean foodInfo;
    private RecyclerView recyclerView;
    private FoodAdapter foodAdapter;
    private BaseActivity baseActivity;
    private SharedPreferences sharedPreferences;
    private String user_id,id,foodname,food_id,collect,foodprice;
    private ImageButton collect_1;
    private Button button,purchase;
    private TextView foodname1,intro,price;
    private ImageView pic;
    private CommentAdapter commentAdapter;
    public FoodInfoActivity(){

    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState);
        setContentView(R.layout.food_adapter);
        SharedPreferences sharedPreferences = getSharedPreferences("lxx",MODE_PRIVATE);
        user_id = sharedPreferences.getString("user_id","user_id");
        id = getIntent().getStringExtra("shop_id");
        foodname = getIntent().getStringExtra("foodname");
        food_id = getIntent().getStringExtra("food_id");
        Toast.makeText(FoodInfoActivity.this,food_id,Toast.LENGTH_SHORT).show();
        initViews();
        initData(food_id);

    }

    void initData(String food_id){
        FoodModel foodModel = new FoodModel();
        foodModel.getFoodById(food_id,this);//访问服务器请求
    }
    @Override
    public void onSuccess(FoodBean foodInfo, int flag) {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        this.foodprice=foodInfo.getPrice();
        this.foodInfo=foodInfo;
        initData1();
    }

    @Override
    public void onFailure(String msg) {
        Toast.makeText(FoodInfoActivity.this,"网络连接失败",Toast.LENGTH_SHORT).show();
    }

    //生成Menu的对象
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //创建菜单
        getMenuInflater().inflate(R.menu.food_list_collect,menu);
        this.menu=menu;
        //菜单创建完毕后向服务器发送请求，判断是否已经收藏
        menu.findItem(R.id.item_collect1).setTitle(foodname);
        isCollected(menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_collect:
                String title = item.getTitle().toString();
                if (title.equals("收藏"))
                {
                    collectFood(menu);
                    collect_1.setImageResource(R.drawable.tab_find_collect_press);
                }
                else
                {
                    uncollectFood(menu);
                    collect_1.setImageResource(R.drawable.tab_find_collect_normal);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initData1(){
        foodname1.setText(foodInfo.getFoodname());
        price.setText(foodInfo.getPrice()+"元");
        intro.setText(foodInfo.getIntro());
        Picasso.with(mContext).load(UserModel.Constants.BASE_URL+foodInfo.getPic()).into(pic);
        collect_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (menu.findItem(R.id.item_collect).getTitle().equals("收藏"))
                {
                    //收藏
                    collectFood(menu);
                    collect_1.setImageResource(R.drawable.tab_find_collect_press);
                }
                else
                {
                    //取消收藏
                    uncollectFood(menu);
                    collect_1.setImageResource(R.drawable.tab_find_collect_normal);
                }
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getAllCommentsByFood(food_id);
            }
        });
        purchase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FoodInfoActivity.this,Purchase.class);
                intent.putExtra("user_id",user_id);
                intent.putExtra("food_id",food_id);
                intent.putExtra("price",foodInfo.getPrice());
                startActivity(intent);
            }
        });

    }

     void initViews(){
        foodname1=(TextView) findViewById(R.id.collect_name);
        intro=(TextView) findViewById(R.id.food_intro1);
        pic=(ImageView)findViewById(R.id.food_pic1);
        price=(TextView) findViewById(R.id.price1);
        collect_1= findViewById(R.id.collect_button);
         button=findViewById(R.id.button4);
         purchase=findViewById(R.id.purchase);
         recyclerView = findViewById(R.id.food_comment);
    }

    @Override
    void initEvents() {

    }

    @Override
    void initData() {

    }

    private void isCollected(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("lxx",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.isCollect(user_id,food_id,"1",menu);
    }

    private void collectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("lxx",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.userCollectFood(user_id,food_id,menu);

    }

    private void uncollectFood(Menu menu){
        String food_id = getIntent().getStringExtra("food_id");
        SharedPreferences sharedPreferences = getSharedPreferences("lxx",MODE_PRIVATE);
        String user_id = sharedPreferences.getString("user_id","user_id");
        CollectModel collectModel = new CollectModel();
        collectModel.unCollectFood_01(user_id,food_id,menu);
    }
    public void getAllCommentsByFood(String food_id){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ShopService foodService = retrofit.create(ShopService.class);
        Call<List<comments>> call = foodService.getAllCommentsByFood(food_id);
        Callback<List<comments>> callback = new Callback<List<comments>>() {
            @Override
            public void onResponse(Call<List<comments>> call, Response<List<comments>> response) {
                List<comments> commentsInfo =  response.body();
                setCommentAdapter(commentsInfo);
            }

            @Override
            public void onFailure(Call<List<comments>> call, Throwable t) {
                Toast.makeText(FoodInfoActivity.this,"网络失败",Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);
    }
    private void setCommentAdapter(List<comments> commentsInfo){
        commentAdapter=new CommentAdapter(this,commentsInfo);
        recyclerView.setAdapter(commentAdapter);
    }

}

